import { createRouter, createWebHistory } from "vue-router";
import { userStore } from "@/store/modules/user";
import NProgress from "nprogress"; // 进度条
import "nprogress/nprogress.css";

let routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login.vue"),
  },
  {
    path: "/",
    name: "home",
    component: () => import("../views/layout/index.vue"),
    children: [
      {
        path: "/brand",
        name: "brand",
        meta: {
          title: "品牌",
        },
        component: () => import("../views/setting/brand/index.vue"),
      },
      {
        path: "/area",
        name: "area",
        meta: {
          title: "区域",
        },
        component: () => import("../views/setting/area/index.vue"),
      },
      {
        path: "/shop",
        name: "shop",
        meta: {
          title: "店铺",
        },
        component: () => import("../views/setting/shop/index.vue"),
      },
      {
        path: "/department",
        name: "department",
        meta: {
          title: "部门职务",
        },
        component: () => import("../views/setting/department/index.vue"),
      },
      {
        path: "/role",
        name: "role",
        meta: {
          title: "角色配置",
        },
        component: () => import("../views/setting/role/index.vue"),
      },
      {
        path: "/role/add",
        name: "roleAdd",
        meta: {
          title: "角色-新增",
        },
        component: () => import("../views/setting/role/add.vue"),
      },
      {
        path: "/user",
        name: "user",
        component: () => import("../views/setting/user/index.vue"),
      },
      {
        path: "/user/add",
        name: "userAdd",
        component: () => import("../views/setting/user/add.vue"),
      },
      {
        path: "/target/sale",
        name: "targetSale",
        meta: {
          title: "销售（考核）目标",
        },
        component: () => import("../views/setting/target/sale/index.vue"),
      },
      {
        path: "/target/sale/add",
        name: "targetSaleAdd",
        meta: {
          title: "新增销售（考核）目标",
          activeMenu: "/target/sale",
        },
        component: () => import("../views/setting/target/sale/add.vue"),
      },
      {
        path: "/warn",
        name: "warn",
        meta: {
          title: "预警配置",
        },
        component: () => import("../views/setting/warn/index.vue"),
      },
      {
        path: "/warn/manage",
        name: "warnManage",
        meta: {
          title: "预警管理",
        },
        component: () => import("../views/setting/warn/manage.vue"),
      },
      {
        path: "/warn/add",
        name: "warnAdd",
        meta: {
          title: "预警配置",
          activeMenu: "/warn",
        },
        component: () => import("../views/setting/warn/add.vue"),
      },
      {
        path: "/task",
        name: "task",
        meta: {
          title: "任务管理",
        },
        component: () => import("../views/setting/task/index.vue"),
      },
      {
        path: "/task/my",
        name: "taskMy",
        component: () => import("../views/setting/task/my.vue"),
      },
      {
        path: "/task/add",
        name: "taskAdd",
        meta: {
            title: "新增任务",
            activeMenu: "/task",
          },
        component: () => import("../views/setting/task/add.vue"),
      },
      {
        path: "/task/merge",
        name: "taskMerge",
        component: () => import("../views/setting/task/merge.vue"),
      },
      {
        path: "/task/handle",
        name: "taskHandle",
        meta: {
          title: "任务处理",
          activeMenu: "/task/my",
        },
        component: () => import("../views/setting/task/handle.vue"),
      },

    ],
  },
  
];
// 路由
const router = createRouter({
  history: createWebHistory(),
  routes,
});
let token = true;

//路由白名单
const Whitelist = ["/", "/login", "/error", "/404"];

//全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title =   "锐刻科技 - " + to.meta.title ;
  }
  const store = userStore();
  NProgress.start();
  //   const hasetoken = getToken("token");
  // if (to.path == "/login" && token) {
  //   //如果已经登录还在访问登录页，直接跳转首页
  //   next("/");
  // }
  if (Whitelist.indexOf(to.path) == -1) {
    //没有在白名单里面
    if (store.menuList.length == 0 && token) {
    } else {
      NProgress.done();
    }
    NProgress.done();
    next();
  } else {
    NProgress.done();
    next();
  }
});
// 导出
export default router;
